<?php
namespace app\blog\controller;
use think\Db;

class Code extends Base {

    public function articleList()
    {
        $curr_page = input('param.pageIndex',1);
        $perpage = input('param.pageSize',10);
        $search = input('param.search','');
        $oriented = input('param.oriented','');
        $cate_id = input('param.cate_id','');
        $whereArticle = [
            ['a.status','=',1],
            ['a.del','=',0]
        ];
        if($search) {
            $whereArticle[] = ['a.title','like',"%{$search}%"];
        }
        if($oriented) {
            $whereArticle[] = ['a.oriented','=',$oriented];
        }
        if($cate_id) {
            $whereArticle[] = ['a.cate_id','=',$cate_id];
        }
        try {
            $count = Db::table('mp_article')->alias('a')->where($whereArticle)->count();
            $list = Db::table('mp_article')->alias('a')
                ->join('mp_article_cate t','a.cate_id=t.id','left')
                ->where($whereArticle)
                ->field('a.id,a.title,a.desc,a.pic,a.create_time,t.cate_name')
                ->order(['a.id'=>'DESC'])
                ->limit(($curr_page-1)*$perpage,$perpage)
                ->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        return ajax([
            'count' => $count,
            'list' => $list
        ]);
    }

    public function conditionList() {
        try {
            $whereOriented = [
                ['del','=',0]
            ];
            $whereCate = [
                ['status','=',1],
                ['del','=',0]
            ];
            $oriented = Db::table('mp_article_oriented')->where($whereOriented)->select();
            $cate_list = Db::table('mp_article_cate')->where($whereCate)->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(), -1);
        }
        $data = [
            'oriented' => $oriented,
            'cate_list' => $cate_list
        ];
        return ajax($data);

    }


}